//
//  EFGoodsDetailViewCellDescription.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/21.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFGoodsDetailViewCellDescription.h"
#import "EFGoodsDetailViewItemDescription.h"

@interface EFGoodsDetailViewCellDescription()<UIWebViewDelegate>

@property(strong,nonatomic)UIWebView* descriptionWebView;

@end

@implementation EFGoodsDetailViewCellDescription


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}
 

-(void)setObject:(EFGoodsDetailViewItemDescription *)item{
    [self.descriptionWebView loadHTMLString:[EFGoodsDetailViewCellDescription loadBody:item.dec] baseURL:nil];
//    [self.descriptionWebView sizeToFit];
}

-(void)initView{
    [self.contentView addSubview:self.descriptionWebView];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    CGFloat documentHeight= [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
    
    
    self.height = documentHeight;
    self.descriptionWebView.frame = CGRectMake(0, 0, kScreenWidth, documentHeight);
     
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEW_HEIGHT" object:self userInfo:nil];
}

-(UIWebView *)descriptionWebView{
    if(_descriptionWebView == NULL){
        _descriptionWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 10)];

        _descriptionWebView.dataDetectorTypes = UIDataDetectorTypeAll;
        _descriptionWebView.left = 20;
        _descriptionWebView.top = 10;
        _descriptionWebView.scrollView.bounces = NO;
        _descriptionWebView.scrollView.scrollEnabled = NO;
//        _descriptionWebView.scalesPageToFit=YES;
        _descriptionWebView.delegate=self;
    }
    return _descriptionWebView;
}


+(NSString*)loadBody:(NSString *)contentStr
{
    if (!contentStr && contentStr.length < 0 ) {
        return @"";
    }
    
    CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
    
    NSString *result = [NSString stringWithFormat:@"<%@ %@",@"img",@" style=\"display: block; max-width: 100%;\""];
    
    
    
    contentStr = [contentStr stringByReplacingOccurrencesOfString:@"<img" withString:result];
    
    contentStr = [self string:contentStr replaceStr:@"<div>"];
    
    contentStr = [self getHtmlString:contentStr] ;
    
    CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    
    NSLog(@"%f", end - start);
    
    
    return contentStr;
}


+ (NSString  *)string:(NSString *)string  replaceStr:(NSString *)replaceStr{
    
    NSRegularExpression *regularExp = [NSRegularExpression regularExpressionWithPattern:@"<div{1}.*?\\>{1}"options:0 error:nil];
    string  = [regularExp stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, string.length) withTemplate:replaceStr];
    return string;
}


+ (NSString *)getHtmlString:(NSString *) body
{
    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendString:@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;\" name=\"viewport\" /><meta name=\"apple-mobile-web-app-capable\" content=\"yes\"><meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\">"];
    [html appendString:@"</head>"];
    
    [html appendString:@"<body>"];
    [html appendString:body];
    [html appendString:@"</body>"];
    
    [html appendString:@"</html>"];
    
    return html;
}



@end
